package com.zmn.plat.services.impl.pilot;

import com.zmn.common.utils.date.DateUtil;
import com.zmn.plat.model.entity.pilot.ChannelPrice;
import com.zmn.plat.model.entity.pilot.ChannelPriceQuery;
import com.zmn.plat.model.entity.pilot.ChannelPriceTO;
import com.zmn.plat.persistence.interfaces.pilot.ChannelPriceDao;
import com.zmn.plat.services.interfaces.pilot.ChannelPriceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import com.zmn.common.constant.GlobalConsts;

/**
 * 描述: 渠道试点接口实现类
 *
 * @author guoqiao
 * @since  2021-06-25 10:16:42
 */
@Slf4j
@Service
public class ChannelPriceServiceImpl implements ChannelPriceService {

    private static final String TAG = "渠道试点基础服务接口";

    @Resource
    private ChannelPriceDao channelPriceDao;

    @Override
    public ChannelPrice findById(Integer channelPriceId) {

        if (Objects.isNull(channelPriceId)) {
            log.error("[{}],根据id查询数据channelPriceId为null", TAG);
            return null;
        }
        return channelPriceDao.findById(channelPriceId);
    }

    @Override
    public List<ChannelPrice> listPageByQuery(ChannelPriceQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        query.setTotalCount(channelPriceDao.countByQuery(query));
        return channelPriceDao.listPageByQuery(query);
    }

    @Override
    public List<ChannelPrice> listByQuery(ChannelPriceQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return channelPriceDao.listByQuery(query);
    }

    @Override
    @Cacheable(cacheNames = "redis10m", key = "'pilot:channel:listPilotTOByQuery:ChannelPriceTO:'+#p0", unless = "#result == null")
    public List<ChannelPriceTO> listPriceTOByQuery(ChannelPriceTO query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件查询数据,query为null", TAG);
            return null;
        }
        return channelPriceDao.listPriceTOByQuery(query);
    }

    @Override
    public Integer countByQuery(ChannelPriceQuery query) {

        if (Objects.isNull(query)) {
            log.error("[{}],按条件统计数据,query为null", TAG);
            return null;
        }
        return channelPriceDao.countByQuery(query);
    }

    @Override
    public Integer updateByKey(ChannelPrice channelPrice) {

        if (Objects.isNull(channelPrice.getChannelPriceId())){
            log.error("[{}],根据id修改数据,channelPrice为null", TAG);
            return null;
        }
        return channelPriceDao.updateByKey(channelPrice);
    }

    @Override
    public void updateStatusBatch(List<Integer> ids, Integer status, String operator) {

        if (!Objects.equals(status, GlobalConsts.NO) && !Objects.equals(status, GlobalConsts.YES)) {
            log.error("[{}],status状态超出取值范围", TAG);
            return;
        }
        channelPriceDao.updateStatusBatch(ids, status, operator, DateUtil.getNow());
    }

    @Override
    public Integer insert (ChannelPrice channelPrice){

        if (Objects.isNull(channelPrice)){
            log.error("[{}],新增数据,channelPrice为null", TAG);
            return null;
        }
        return channelPriceDao.insert(channelPrice);
    }

    @Override
    public Integer deleteByKey(Integer channelPriceId) {

        if (Objects.isNull(channelPriceId)){
            log.error("[{}],根据id删除数据,channelPriceId为null", TAG);
            return null;
        }
        return channelPriceDao.deleteByKey(channelPriceId);
    }

}